package sorted;

import java.util.Arrays;

public class S01_BubbleSorted {
    public static void main(String[] args) {
        int[] arr = {5,7,2,3,4,1,6,18,9,10,16,56,34,23,11,12,31,17,15};
        int[] sorted = bubbleSort(arr);

        System.out.println("排序后的数组为："+Arrays.toString(sorted));
    }
    public static int[] bubbleSort(int[] arr) {
        int length = arr.length;
        for (int i = 0; i < length; i++) {
            boolean flag = true;

            for (int j = 0; j < length-1; j++) {
                int n1 = arr[j];
                int n2 = arr[j + 1];

                if (n1 > n2) {
                    arr[j] = n2;
                    arr[j + 1] = n1;
                    flag=false;
                }
            }

            if (flag) {
                break;
            }

        }
        return arr;
    }
}
